<?php

namespace App\Http\Controllers\Admin\Personnel;

use App\Http\Controllers\Controller;
use App\Models\AdminAuthModel;
use App\Models\AdminModel;
use App\Models\AdminRoleModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//职位
class Role extends Controller
{

    public function index(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $name = trim($request->input('name', ''));
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = AdminRoleModel::query();
        
        if($name) {  $query->where('name', 'like', "%{$name}%"); }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query->offset(($page - 1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();
        $list = [];

        $list = $items->map(function($item) {
            $auth = json_decode($item->auth);
            if($item->auth == '*') {
                $auth = AdminAuthModel::pluck('id');
            }
            $res = [
                'id' => $item->id,
                'name' => $item->name,
                'auth' => $auth,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->admin->name ?? '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function add(Request $request) {
        $auth = $request->get('auth');
        $params = $request->all();
        $validator = Validator::make($params, [
            'name' => 'required',
//            'auth' => 'required|array',
        ], [
            'name.required' => '请填写名称',
//            'auth.required' => '请选择权限',
//            'auth.array' => '权限错误',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

//        $final_auth = $params['auth'];
//        foreach($params['auth'] as $auth_id) {
//            //获取上级权限
//            $pid = AdminAuthModel::where('id', $auth_id)->value('pid');
//            while($pid != 0) {
//                $final_auth[] = $pid;
//                $pid = AdminAuthModel::where('id', $pid)->value('pid');
//            }
//        }
//        $final_auth = array_unique($final_auth);

        $role = new AdminRoleModel();
        $role->name = $params['name'];
//        $role->auth = json_encode($params['auth']);
        $role->created_admin_id = $auth->id;
        $role->save();
        return $this->success();
    }

    public function update(Request $request, int $id){
        $params = $request->all();
        $validator = Validator::make($params, [
            'name' => 'required',
//            'auth' => 'required|array',
        ], [
            'name.required' => '请填写名称',
//            'auth.required' => '请选择权限',
//            'auth.array' => '权限错误',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $role = AdminRoleModel::find($id);
        if(!$role) {
            return $this->error('角色错误');
        }

        $role->name = $params['name'];
//        $role->auth = json_encode($params['auth']);
        $role->save();
        return $this->success();
    }

    public function delete(int $id){
        $role = AdminRoleModel::find($id);
        if(!$role) {
            return $this->error('角色错误');
        }
        AdminRoleModel::destroy($id);

        return $this->success();
    }

}
